
let params={
  pagenum:1,
  pagesize:2,
}


function renderArticle() {
  axios.get('/my/article/list',{params}).then(res =>{
    let { status, data, total}=res.data;
    if(status ===0){
      let str='';
      data.forEach(item => {
        str +=`
        <tr>
            <td>${item.title}</td>
            <td>${item.cate_name}</td>
            <td>${item.pub_date}</td>
            <td>${item.state}</td>
            <td>
              <button type="button" class="layui-btn layui-btn-xs">编辑</button>
              <button type="button" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
            </td>
          </tr>`
      });
      $('tbody').html(str);
      showPage(total)
    }
    }
  )
}
renderArticle()

let  laypage = layui.laypage;
function showPage(t) {
  //执行一个laypage实例
  laypage.render({
    elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号
    count: t, //数据总数，从服务端得到
    limit:params.pagesize,
    curr:params.pagenum,
    limits:[2,3,4,5,6,7],
    layout:['limit','prev','page','next','count','skip'],
    jump:function (obj, first) {
      if(!first){
        params.pagenum=obj.curr;
        params.pagesize=obj.limit;
        renderArticle()
      }
    }
  });
};

// 获取筛选
axios.get('/my/category/list').then(res=> {
  let { status, data}= res.data;
  if( status=== 0){
    let str='';
    data.forEach(item =>{
      str+=`
      <option value="${item.id}">${item.name}</option>`
    })
    $('#category').append(str);
    let form= layui.form;
    form.render('select')
  }
})


// 完成筛选
$('#search').on('click',function (e) {
  e.preventDefault();
  let cate_id =$('#category').val();
  let state =$('#state').val();
  if(cate_id){
    params.cate_id=cate_id;
  } else{
    delete params.cate_id;
  }

  if(state){
    params.state=state;
  } else{
    delete params.state;
  }
  params.pagenum=1;
  renderArticle()
})